//
//  RoomInfoTableViewCell.m
//  OutesIOS
//
//  Created by norman on 2018/11/18.
//  Copyright © 2018 norman. All rights reserved.
//

#import "RoomInfoTableViewCell.h"

#define CELL_PADDING_LR 15
#define CELL_PADDING_TB 5

@interface RoomInfoTableViewCell()

@property (nonatomic,strong)UIView *bgView;

@property (nonatomic,strong)UILabel *lbName;

@property (nonatomic,strong)UILabel *lbArea;

@property (nonatomic,strong)UILabel *lbHeat;

@property (nonatomic,strong)UILabel *lbCold;

@end

@implementation RoomInfoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.contentView.backgroundColor = UIColor16(0xeeeeee);
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(CELL_PADDING_TB, CELL_PADDING_TB, kScreen_Width - 2*CELL_PADDING_TB, 0)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 4;
    //阴影效果
//    self.bgView.layer.shadowColor = UIColor16(0x000000).CGColor;
//    self.bgView.layer.shadowOffset = CGSizeMake(2, 4);
//    self.bgView.layer.shadowOpacity = 0.5;
//    self.bgView.layer.shadowRadius = 4;
    [self.contentView addSubview:self.bgView];
    
    //编辑图标
    UIImageView *edit = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"edit"]];
    edit.x = self.bgView.width - edit.width - CELL_PADDING_LR;
    [self.bgView addSubview:edit];
    
    //房间名称
    self.lbName = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"客厅"];
    self.lbName.textColor = BSBlackTextColor;
    self.lbName.x = CELL_PADDING_LR;
    self.lbName.y = CELL_PADDING_LR;
    edit.centerY = self.lbName.centerY;
    [self.bgView addSubview:self.lbName];
    
    //面积
    self.lbArea = [UILabel labelSingleLineFontSize:kListDetailFontSize content:@"200 ㎡"];
    self.lbArea.textColor = CM_TABLE_DETAIL_COLOR;
    self.lbArea.width += 100;
    self.lbArea.x = CGRectGetMaxX(self.lbName.frame) + 5;
    self.lbArea.y = CGRectGetMaxY(self.lbName.frame) - self.lbArea.height;
    [self.bgView addSubview:self.lbArea];
    
    UILabel *lb1 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"冷负荷："];
    lb1.textColor = CM_TABLE_DETAIL_COLOR;
    lb1.x = CELL_PADDING_LR;
    lb1.y = CGRectGetMaxY(self.lbName.frame) + 10;
    [self.bgView addSubview:lb1];
    
    self.lbCold = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"300 W"];
    self.lbCold.width += 50;
    self.lbCold.textColor = CM_TABLE_TITLE_COLOR;
    self.lbCold.x = CGRectGetMaxX(lb1.frame) + 2;
    self.lbCold.y = lb1.y;
    [self.bgView addSubview:self.lbCold];
    
    UILabel *lb2 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"热负荷："];
    lb2.textColor = CM_TABLE_DETAIL_COLOR;
    lb2.x = self.bgView.width/2;
    lb2.y = lb1.y;
    [self.bgView addSubview:lb2];
    
    self.lbHeat = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"200 W"];
    self.lbHeat.textColor = CM_TABLE_TITLE_COLOR;
    self.lbHeat.x = CGRectGetMaxX(lb2.frame) + 2;
    self.lbHeat.y = lb2.y;
    self.lbHeat.width += 50;
    [self.bgView addSubview:self.lbHeat];
    
    self.bgView.height = CGRectGetMaxY(self.lbHeat.frame) + CELL_PADDING_LR;
}

- (void)setRoomData:(RoomData*)room andDevices:(NSArray*)devices {
    self.lbName.text = room.room_name;
    self.lbName.width = [Tools sizeOfSingleLineText:room.room_name withSize:kActionBarFontSize].width + 2;
    self.lbArea.x = CGRectGetMaxX(self.lbName.frame) + 3;
    NSString *area = [NSString stringWithFormat:@"%.1f ㎡", room.area];
    NSString *cold = [NSString stringWithFormat:@"%.1fW", room.cold_load];
    NSString *heat = [NSString stringWithFormat:@"%.1fW", room.heat_load];
    self.lbArea.text = area;
    self.lbCold.text = cold;
    self.lbHeat.text = heat;
    
    for (UIView *sub in self.bgView.subviews) {
        if (sub.tag >= 100) {
            [sub removeFromSuperview];
        }
    }
    
    CGFloat y = CGRectGetMaxY(self.lbHeat.frame) + 10;
    if (devices != nil && devices.count > 0) {
        UILabel *lb2 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"室内机："];
        lb2.textColor = CM_TABLE_DETAIL_COLOR;
        lb2.tag = 100;
        lb2.x = CELL_PADDING_LR;
        lb2.y = y;
        [self.bgView addSubview:lb2];
        for (int i = 0; i < devices.count; i++) {
            RoomEquipData *data = [devices objectAtIndex:i];
            UILabel *device = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"MCW200VC1 X 1台"];
            device.attributedText = [self getDeviceText:data.equip_model andNumber:data.count];
            device.x = CGRectGetMaxX(lb2.frame) + 2;
            device.y = y;
            device.tag = 200 + i;
            device.width = self.bgView.width - 2*CELL_PADDING_LR;
            [self.bgView addSubview:device];
            y = CGRectGetMaxY(device.frame) + 10;
        }
    }
    
    if ([room.terminal_type containsString:@"地暖"] && room.pipe_length > 0) {
        UILabel *lb2 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"地热盘管："];
        lb2.textColor = CM_TABLE_DETAIL_COLOR;
        lb2.tag = 1000;
        lb2.x = CELL_PADDING_LR;
        lb2.y = y;
        [self.bgView addSubview:lb2];
        
        NSString *info = [NSString stringWithFormat:@"管径 %dmm", room.pipe_diameter];
        UILabel *lbDrpg = [UILabel labelSingleLineFontSize:kActionBarFontSize content:info];
        lbDrpg.x = CGRectGetMaxX(lb2.frame) + 2;
        lbDrpg.y = y;
        lbDrpg.tag = 1001;
        lbDrpg.textColor = CM_TABLE_TITLE_COLOR;
        [self.bgView addSubview:lbDrpg];
        
        info = [NSString stringWithFormat:@"间距 %dmm", room.pipe_space];
        UILabel *lbDrpg1 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:info];
        lbDrpg1.x = (kScreen_Width - lbDrpg.x)/2 + lbDrpg.x;
        lbDrpg1.y = y;
        lbDrpg1.tag = 1002;
        lbDrpg1.textColor = CM_TABLE_TITLE_COLOR;
        [self.bgView addSubview:lbDrpg1];
        y = CGRectGetMaxY(lbDrpg.frame) + 10;
        
        info = [NSString stringWithFormat:@"长度 %dm", room.pipe_length];
        UILabel *lbDrpg2 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:info];
        lbDrpg2.x = lbDrpg.x;
        lbDrpg2.y = y;
        lbDrpg2.tag = 1003;
        lbDrpg2.textColor = CM_TABLE_TITLE_COLOR;
        [self.bgView addSubview:lbDrpg2];
        
        info = [NSString stringWithFormat:@"路数 %d", room.pipe_count];
        UILabel *lbDrpg3 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:info];
        lbDrpg3.x = lbDrpg1.x;
        lbDrpg3.y = y;
        lbDrpg3.tag = 1004;
        lbDrpg3.textColor = CM_TABLE_TITLE_COLOR;
        [self.bgView addSubview:lbDrpg3];
        y = CGRectGetMaxY(lbDrpg2.frame) + CELL_PADDING_LR;
        self.bgView.height = y;
    } else {
        self.bgView.height = y + 5;
    }
    self.contentView.height = self.bgView.height + 2*CELL_PADDING_TB;
}

- (NSMutableAttributedString*)getDeviceText:(NSString*)device andNumber:(long)number {
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  X  %ld台", device, number]];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:kActionBarFontSize]
                          range:NSMakeRange(0, AttributedStr.length)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:CM_TABLE_TITLE_COLOR
                          range:NSMakeRange(0, AttributedStr.length)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:CM_ORANGE_COLOR
                          range:NSMakeRange([device length], 4)];
    return AttributedStr;
}

+ (CGFloat)cellHeight:(NSInteger)number hasDrpg:(BOOL)drpg {
    CGFloat h = 2*CELL_PADDING_TB;
    if (drpg) {
        h += (4 + number)*(10 + [Tools sizeOfSingleLineText:@"高度" withSize:kActionBarFontSize].height) + 2*CELL_PADDING_LR - 10;
    } else {
        h += (2 + number)*(10 + [Tools sizeOfSingleLineText:@"高度" withSize:kActionBarFontSize].height) + 2*CELL_PADDING_LR -10;
    }
//
//    CGFloat h = 2*CELL_PADDING_TB;
//    h += 3*(10 + [Tools sizeOfSingleLineText:@"高度" withSize:kActionBarFontSize].height) + 2*CELL_PADDING_LR - 10;
//
    return h;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
